package com.ebt.m.msgnote;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.commons.buscomponent.listview.IView;
import com.ebt.m.commons.utils.BitmapUtil;
import com.ebt.m.msgnote.bean.TypeMsgItems;
import com.ebt.m.utils.imageloader.ImageMgr;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author allen
 * Created by allen on 2018/7/23 18:41.
 */
public class TypeMsgItemView extends IView {

    @BindView(R.id.tv_msg_titleIcon)
    ImageView tvMsgTitleIcon;
    @BindView(R.id.rl_icon_msg)
    RelativeLayout rlIconMsg;
    @BindView(R.id.tv_msg_title)
    TextView tvMsgTitle;
    @BindView(R.id.tv_msg_desc)
    TextView tvMsgDesc;
    @BindView(R.id.tv_msg_arrow)
    ImageView tvMsgArrow;
    @BindView(R.id.rlItemViewMsg)
    RelativeLayout rlItemViewMsg;
    @BindView(R.id.ivCircleRed)
    ImageView ivCircleRed;
    TypeMsgItems.TypeMsg bean;

    public TypeMsgItemView(Context context) {
        this(context, null);
        ButterKnife.bind(this);
    }

    public TypeMsgItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TypeMsgItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inflate(context, R.layout.msg_type_itemview, this);
    }

    @Override
    public void update(Object... args) {
        bean = (TypeMsgItems.TypeMsg) args[0];
        int jumpMode = bean.getJumpMode();
        int status = bean.getStatus();
        if (status == MsgConstant.MSG_UNREAD) {
            ivCircleRed.setVisibility(VISIBLE);
        } else {
            ivCircleRed.setVisibility(GONE);
        }
        ImageMgr.getInstance().displayImage(tvMsgTitleIcon, bean.getIcon(), "");
        tvMsgTitle.setText(bean.getTitle());
        String content = bean.getContent();
        if (TextUtils.isEmpty(content)) {
            tvMsgDesc.setText("");
            tvMsgDesc.setVisibility(GONE);
        } else {
            tvMsgDesc.setVisibility(VISIBLE);
            tvMsgDesc.setText(content);
        }
        if (jumpMode == MsgConstant.JUMP_MODE_NO) {
            tvMsgArrow.setVisibility(GONE);
        } else {
            tvMsgArrow.setVisibility(GONE);
        }
    }

    private void setIcon(int unReadCount, int msg_customer_normal) {
        if (unReadCount == 0) {
            tvMsgTitleIcon.setImageResource(msg_customer_normal);
        } else {
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), msg_customer_normal);
            tvMsgTitleIcon.setImageBitmap(BitmapUtil.generatorNumIcon3(getContext(), bmp, true, unReadCount + ""));
        }
    }

    @OnClick(R.id.rlItemViewMsg)
    public void onViewClicked() {
        mOnclickListner.onClick(rlItemViewMsg, bean);
        ivCircleRed.setVisibility(GONE);
    }
}
